﻿using Ext.Net;
using NOAH.CMS.Repository;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;

public partial class admin_Modules_NewsEvent_Dialog_NewsCategoryList : System.Web.UI.Page
{

    private List<NewsCategory> allCategories = new List<NewsCategory>();

    private string selectedIds = string.Empty;



    public TreeNodeCollection CreateNode(NewsCategory product)
    {
        TreeNodeCollection nodes = new TreeNodeCollection();
        TreeNode node = new TreeNode
        {
            Text = product.Name,
            NodeID = product.ID.ToString(),
            SingleClickExpand = false,
            Expanded = true
        };
        if (("," + this.hdfParentCategoryID.Value.ToString() + ",").Contains("," + node.NodeID + ","))
        {
            node.Checked = ThreeStateBool.True;
        }
        else
        {
            node.Checked = ThreeStateBool.False;
        }
        List<NewsCategory> childCategories = (from p in this.allCategories
                                              where p.ParentId == product.ID
                                              select p).ToList<NewsCategory>();
        foreach (NewsCategory p in childCategories)
        {
            node.Nodes.AddRange(this.CreateNode(p));
        }
        if (childCategories.Count > 0)
        {
            node.Leaf = false;
        }
        else
        {
            node.Leaf = true;
        }
        nodes.Add(node);
        return nodes;
    }

    [DirectMethod]
    public string NodeLoad(string nodeID)
    {
        string text1 = base.Request.QueryString["cateID"];
        if (text1 == null)
        {
        }
        this.selectedIds = string.Empty;
        this.hdfParentCategoryID.Value = this.selectedIds;
        TreeNodeCollection nodes = new TreeNodeCollection();
        if (nodeID == "0")
        {
            this.allCategories = new NewsCategoryRepository().GetAll();
            List<NewsCategory> topCategories = this.allCategories.Where<NewsCategory>(delegate(NewsCategory p)
            {
                int parentId = p.ParentId;
                return (p.ParentId == 0);
            }).ToList<NewsCategory>();
            foreach (NewsCategory category in topCategories)
            {
                nodes.AddRange(this.CreateNode(category));
            }
        }
        return nodes.ToJson();
    }

    public void OnSelect(object sender, DirectEventArgs e)
    {
        string parameterFormat = "'{0}', '{1}'";
        string text = (this.hdfParentText.Value != null) ? this.hdfParentText.Value.ToString() : string.Empty;
        string value = (this.hdfParentCategoryID.Value != null) ? this.hdfParentCategoryID.Value.ToString() : string.Empty;
        string parameter = string.Format(parameterFormat, text, value);
        BasePage.RegisterCallBackFromChild(this.ScriptManager2, parameter);
        ExtNet.Msg.Hide();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (ExtNet.IsAjaxRequest)
        {
        }
    }
}